<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=drawing"></script>

 <script type="text/javascript">
      var drawingManager;
      var selectedShape;
      var colors = ['#1E90FF', '#FF1493', '#32CD32', '#FF8C00', '#4B0082'];
      var selectedColor;
      var colorButtons = {};

      function clearSelection() {
        if (selectedShape) {
          selectedShape.setEditable(false);
          selectedShape = null;
        }
      }

      function setSelection(shape) {
        clearSelection();
        selectedShape = shape;
        shape.setEditable(true);
        selectColor(shape.get('fillColor') || shape.get('strokeColor'));
      }

      function deleteSelectedShape() {
        if (selectedShape) {
          selectedShape.setMap(null);
        }
      }

      function selectColor(color) {
        selectedColor = color;
        for (var i = 0; i < colors.length; ++i) {
          var currColor = colors[i];
          colorButtons[currColor].style.border = currColor == color ? '2px solid #789' : '2px solid #fff';
        }

        // Retrieves the current options from the drawing manager and replaces the
        // stroke or fill color as appropriate.
        var polylineOptions = drawingManager.get('polylineOptions');
        polylineOptions.strokeColor = color;
        drawingManager.set('polylineOptions', polylineOptions);


        var rectangleOptions = drawingManager.get('rectangleOptions');
        rectangleOptions.fillColor = color;
        drawingManager.set('rectangleOptions', rectangleOptions);

        var circleOptions = drawingManager.get('circleOptions');
        circleOptions.fillColor = color;
        drawingManager.set('circleOptions', circleOptions);


        var polygonOptions = drawingManager.get('polygonOptions');
        polygonOptions.fillColor = color;
        drawingManager.set('polygonOptions', polygonOptions);
      }

      function setSelectedShapeColor(color) {
        if (selectedShape) {
          if (selectedShape.type == google.maps.drawing.OverlayType.POLYLINE) {
            selectedShape.set('strokeColor', color);
          } else {
            selectedShape.set('fillColor', color);
          }
        }
      }

      function makeColorButton(color) {
        var button = document.createElement('span');
        button.className = 'color-button';
        button.style.backgroundColor = color;
        google.maps.event.addDomListener(button, 'click', function() {
          selectColor(color);
          setSelectedShapeColor(color);
        });

        return button;
      }

       function buildColorPalette() {
         var colorPalette = document.getElementById('color-palette');
         for (var i = 0; i < colors.length; ++i) {
           var currColor = colors[i];
           var colorButton = makeColorButton(currColor);
           colorPalette.appendChild(colorButton);
           colorButtons[currColor] = colorButton;
         }
         selectColor(colors[0]);
       }

      function initialize() {
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 10,
          center: new google.maps.LatLng(-6.907682,107.610397),
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          disableDefaultUI: true,
          zoomControl: true
        });

        var polyOptions = {
          strokeWeight: 0,
          fillOpacity: 0.45,
          editable: true
        };
        // Creates a drawing manager attached to the map that allows the user to draw
        // markers, lines, and shapes.
        drawingManager = new google.maps.drawing.DrawingManager({
          drawingMode: google.maps.drawing.OverlayType.POLYGON,
          markerOptions: {
            draggable: true
          },
          polylineOptions: {
            editable: true
          },
          
          rectangleOptions: polyOptions,
          circleOptions: polyOptions, 
          polygonOptions: polyOptions,
          map: map
        });

        google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
            if (e.type != google.maps.drawing.OverlayType.MARKER) {
            // Switch back to non-drawing mode after drawing a shape.
            drawingManager.setDrawingMode(null);

            // Add an event listener that selects the newly-drawn shape when the user
            // mouses down on it.
            var newShape = e.overlay;
            newShape.type = e.type;
            google.maps.event.addListener(newShape, 'click', function() {
              setSelection(newShape);
            });
            setSelection(newShape);
          }
        });

        // Clear the current selection when the drawing mode is changed, or when the
        // map is clicked.
        google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection);
        google.maps.event.addListener(map, 'click', clearSelection);
        google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);

        buildColorPalette();
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
<?php

$o = "";
	
if (isset($_GET['success']) && ($_GET['success'] == "1")) {

	$o .= '<div class="alert alert-success">
			Proses tambah kegiatan berhasil
			</div>';
			
} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
	
	$o .= '<div class="alert alert-error">
			Proses tambah kegiatan gagal
		   </div>';
}
		
echo $o;

?>
<div class="row">
<div class="span8">
	<div class="control-group">
	 <div id="map" style="width:100%; height:400px"></div>
	</div>
<a href="?page=kegiatan"><i class="  icon-arrow-left"></i> kembali</a>
</div>
	
	<form action="?page=addkegiatan&action=add" method="POST"> 
	<div class="span4">
	<div class="control-group">
	  <label class="control-label" for="input01">Nama Kegiatan</label>
	  <div class="controls">
		<input type="text" class="input-xlarge" id="kegiatan" name="kegiatan" rel="popover" data-content="Masukkan nama kegiatan." data-original-title="Kegiatan">
	  </div>
	</div>
	
	<div class="control-group">
	  <label class="control-label" for="input01">Kabupaten</label>
	  <div class="controls">
		<select name="kabupaten" id="kabupaten" onchange="setpeta(this.options[this.selectedIndex].value)" data-placeholder="Pilih kabupaten..." class="chzn-select" style="width:280px;" tabindex="2">
		<?php
		
		/* memanggil fungsi untuk menampilkan data kabupaten */
		$kabupaten	= getKab(); 
		
		/* menampilkan data kabupaten */
		if ($kabupaten) {
			$o .= '<option value=""></option>';
			foreach ($kabupaten as $l) {
				$o .= '<option value="'.$l['lat'].",".$l['long'].",".$l['idkabkota'].'">'.$l['nama_kabkota'].'</option>';
				/* echo "<a href=\"javascript:setpeta(".$l['lat'].",".$l['long'].",".$l['idkabkota'].")\">".$l['nama_kabkota']."</a><br>\n";
    			 echo "<span id=\"".$l['idkabkota']."\" style=\"display:none\"></span>\n";*/
			}
		} else {
			$o .= '<option value="">tidak ada data kabupaten</option>';
		}
		
		echo $o;
	
		?>
		</select>
		</div>
	</div>
	
	<div class="control-group">
		<label class="control-label" for="input01">Pagu</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="pagu" name="pagu" rel="popover" data-content="Masukkan pagu" data-original-title="Pagu">
		  </div>
	</div>
	
	<div class="control-group">
		<label class="control-label" for="input01">Longitude</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="longitude" name="longitude" readonly=true>
		  </div>
	</div>
	
	<div class="control-group">
		<label class="control-label" for="input01">Latitude</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="latitude" name="latitude" readonly=true>
		  </div>
	</div>
	
	<div class="control-group">
		<label class="control-label" for="input01"></label>
		  <div class="controls">
		   <button type="submit" class="btn btn-success" rel="tooltip" title="first tooltip">Tambah Kegiatan</button>
		  </div>
	</div>
	</form>

</div>
</div>
<?php


if ($_GET['action'] == "add") {
	
	$nama_kegiatan	= htmlentities(mysql_real_escape_string($_POST['kegiatan']));
	$kabkota		= htmlentities(mysql_real_escape_string($_POST['kabupaten']));
	$sektor			= $_SESSION['sektoral'];
	$pagu			= htmlentities(mysql_real_escape_string($_POST['pagu']));
	$longitude		= htmlentities(mysql_real_escape_string($_POST['longitude']));
	$latitude		= htmlentities(mysql_real_escape_string($_POST['latitude']));
	$tahun			= date('Y');
	$username		= $_SESSION['username'];
	
	$o = "";
	
	$kegiatan = addKegiatan($nama_kegiatan, $sektor, $kabkota, $pagu, $longitude, $latitude, $tahun, $username);
	
	if ($kegiatan) {
		?>
		<script language="javascript">
		document.location="?page=addkegiatan&success=1";
		</script>
		<?php
	} else {
		?>
			<script language="javascript">
			document.location="?page=addkegiatan&success=0";
			</script>
		<?php
	}
	
}





